<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">callbacks.lock()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/callbacks-object/">回调对象</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/callbacks.lock/" target="_blank">callbacks.lock()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="callbacks-lock1"><h2 class="section-title">
<span class="name">callbacks.lock()</span><span class="returns">返回: <a href="/Types/#Callbacks">Callbacks</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>锁定回调列表的当前状态。</p>
<ul class="signatures"><li class="signature" id="callbacks-lock">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.7/">1.7</a></span>callbacks.lock()</h4>
<ul><li><div class="null-signature">这个方法不接受任何参数</div></li></ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p>此方法返回绑定它的那个回调对象(<code>this</code>).</p>
<p>如果回调对象被创建，用<code>"memory"</code>标志作为它的参数，绑定函数可能会在回调列表中被锁定后增加并且触发。
			    	</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4>Example: <span class="desc">
				用 <code>callbacks.lock()</code> 锁定一个回调列表，以避免进一步的修改列表状态 :
				</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="comment">// a sample logging function to be added to a callbacks list</span></code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> foo = <span class="keyword">function</span>( value ) {</code></div></div><div class="container"><div class="line"><code>  console.log( <span class="string">"foo:"</span> + value );</code></div></div><div class="container"><div class="line"><code>};</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> callbacks = $.Callbacks();</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// add the logging function to the callback list</span></code></div></div><div class="container"><div class="line"><code>callbacks.add( foo );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// fire the items on the list, passing an argument</span></code></div></div><div class="container"><div class="line"><code>callbacks.fire( <span class="string">"hello"</span> );</code></div></div><div class="container"><div class="line"><code><span class="comment">// outputs "foo: hello"</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// lock the callbacks list</span></code></div></div><div class="container"><div class="line"><code>callbacks.lock();</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// try firing the items again</span></code></div></div><div class="container"><div class="line"><code>callbacks.fire( <span class="string">"world"</span> );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// as the list was locked, no items</span></code></div></div><div class="container"><div class="line"><code><span class="comment">// were called, so "world" isn"t logged</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-1">
<h4>Example: <span class="desc">Use <code>callbacks.lock()</code> to lock a callback list with "memory," and then resume using the list:</span>
</h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div>
<div class="line n31">31</div>
<div class="line n32">32</div>
<div class="line n33">33</div>
<div class="line n34">34</div>
<div class="line n35">35</div>
<div class="line n36">36</div>
<div class="line n37">37</div>
<div class="line n38">38</div>
<div class="line n39">39</div>
<div class="line n40">40</div>
<div class="line n41">41</div>
<div class="line n42">42</div>
<div class="line n43">43</div>
<div class="line n44">44</div>
<div class="line n45">45</div>
<div class="line n46">46</div>
<div class="line n47">47</div>
<div class="line n48">48</div>
<div class="line n49">49</div>
<div class="line n50">50</div>
<div class="line n51">51</div>
<div class="line n52">52</div>
<div class="line n53">53</div>
<div class="line n54">54</div>
<div class="line n55">55</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span> <span class="attribute">id</span>=<span class="value">"log"</span>&gt;</span><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"><span class="comment">// simple function for logging results</span></span></code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> log = <span class="keyword">function</span>( value) {</code></div></div><div class="container"><div class="line"><code>  $( <span class="string">"#log"</span> ).append( <span class="string">"&lt;p&gt;"</span> + value + <span class="string">"&lt;/p&gt;"</span> );</code></div></div><div class="container"><div class="line"><code>};</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// two sample functions to be added to a callbacks list</span></code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> foo = <span class="keyword">function</span>( value ) {</code></div></div><div class="container"><div class="line"><code>  log( <span class="string">"foo: "</span> + value );</code></div></div><div class="container"><div class="line"><code>};</code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> bar = <span class="keyword">function</span>( value ) {</code></div></div><div class="container"><div class="line"><code>  log( <span class="string">"bar: "</span> + value );</code></div></div><div class="container"><div class="line"><code>};</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// create the callbacks object with the "memory" flag</span></code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> callbacks = $.Callbacks( <span class="string">"memory"</span> );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// add the foo logging function to the callback list</span></code></div></div><div class="container"><div class="line"><code>callbacks.add( foo );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// fire the items on the list, passing an argument</span></code></div></div><div class="container"><div class="line"><code>callbacks.fire( <span class="string">"hello"</span> );</code></div></div><div class="container"><div class="line"><code><span class="comment">// outputs "foo: hello"</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// lock the callbacks list</span></code></div></div><div class="container"><div class="line"><code>callbacks.lock();</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// try firing the items again</span></code></div></div><div class="container"><div class="line"><code>callbacks.fire( <span class="string">"world"</span> );</code></div></div><div class="container"><div class="line"><code><span class="comment">// as the list was locked, no items were called,</span></code></div></div><div class="container"><div class="line"><code><span class="comment">// so "foo: world" isn't logged</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// add the foo function to the callback list again</span></code></div></div><div class="container"><div class="line"><code>callbacks.add( foo );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// try firing the items again</span></code></div></div><div class="container"><div class="line"><code>callbacks.fire( <span class="string">"silentArgument"</span> );</code></div></div><div class="container"><div class="line"><code><span class="comment">// outputs "foo: hello" because the argument value was stored in memory</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="comment">// add the bar function to the callback list</span></code></div></div><div class="container"><div class="line"><code>callbacks.add( bar );</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>callbacks.fire( <span class="string">"youHadMeAtHello"</span> );</code></div></div><div class="container"><div class="line"><code><span class="comment">// outputs "bar: hello" because the list is still locked,</span></code></div></div><div class="container"><div class="line"><code><span class="comment">// and the argument value is still stored in memory</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>